package com.neusoft.neuiotms.dcom.converter.malfunction;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.malfunction.MalfunctionApplyFormDTO;
import com.neusoft.neuiotms.dcom.model.malfunction.MalfunctionApplyForm;
import com.neusoft.neuiotms.dcom.model.malfunction.MalfunctionApplyFormAnnex;
import com.neusoft.neuiotms.dcom.repository.malfunction.MalfunctionApplyFormAnnexRepository;
import com.neusoft.neuiotms.dcom.repository.malfunction.MalfunctionApplyFormRepository;

@Component
public class MalfunctionApplyFormConverter extends AbstractConverter<MalfunctionApplyForm, MalfunctionApplyFormDTO> {

    @Autowired
    private MalfunctionApplyFormRepository malfunctionApplyFormRepository;
    @Autowired
    private MalfunctionApplyFormAnnexRepository malfunctionApplyFormAnnexRepository;
    @Autowired
    private MalfunctionApplyFormAnnexConverter malfunctionApplyFormAnnexConverter;

    @Override
    public MalfunctionApplyForm toModel(final MalfunctionApplyFormDTO dto) {
        MalfunctionApplyForm model;
        if (dto.isNew()) {
            model = new MalfunctionApplyForm();
        } else {
            model = this.malfunctionApplyFormRepository.getOne(dto.getId());
        }
        model.setTitle(dto.getTitle());
        model.setTaskId(dto.getTaskId());
        model.setWorkNum(dto.getWorkNum());
        model.setServiceUnitName(dto.getServiceUnitName());
        model.setServiceUnitCode(dto.getServiceUnitCode());
        model.setFrontlineEngineerCode(dto.getFrontlineEngineerCode());
        model.setFrontlineEngineerName(dto.getFrontlineEngineerName());
        model.setCustomerName(dto.getCustomerName());
        model.setCustomerPhone(dto.getCustomerPhone());
        model.setPlace(dto.getPlace());
        model.setRoomNumber(dto.getRoomNumber());
        model.setWhetherBackup(dto.getWhetherBackup());
        model.setWhetherSecret(dto.getWhetherSecret());
        model.setEventLevel(dto.getEventLevel());
        model.setEventDescription(dto.getEventDescription());
        model.setHelperEngineerCode(dto.getHelperEngineerCode());
        model.setHelperEngineerName(dto.getHelperEngineerName());
        model.setStatus(dto.getStatus());

        final List<MalfunctionApplyFormAnnex> annexs = model.getAnnex().stream().collect(Collectors.toList());

        annexs.forEach(k -> {
            model.getAnnex().remove(k);
            this.malfunctionApplyFormAnnexRepository.delete(k);
        });

        dto.getAnnex().stream().map(s -> {
            final MalfunctionApplyFormAnnex annex = this.malfunctionApplyFormAnnexConverter.toModel(s);
            annex.setMalfunctionApplyForm(model);
            return annex;
        }).forEach(model.getAnnex()::add);

        return model;
    }

    @Override
    public MalfunctionApplyFormDTO toDTO(final MalfunctionApplyForm model, final boolean forListView) {

        final MalfunctionApplyFormDTO dto = new MalfunctionApplyFormDTO();
        dto.setId(model.getId());
        dto.setTitle(model.getTitle());
        dto.setTaskId(model.getTaskId());
        dto.setWorkNum(model.getWorkNum());
        dto.setServiceUnitCode(model.getServiceUnitCode());
        dto.setServiceUnitName(model.getServiceUnitName());
        dto.setFrontlineEngineerCode(model.getFrontlineEngineerCode());
        dto.setFrontlineEngineerName(model.getFrontlineEngineerName());
        dto.setCustomerName(model.getCustomerName());
        dto.setCustomerPhone(model.getCustomerPhone());
        dto.setPlace(model.getPlace());
        dto.setRoomNumber(model.getRoomNumber());
        dto.setWhetherBackup(model.getWhetherBackup());
        dto.setWhetherSecret(model.getWhetherSecret());
        dto.setEventLevel(model.getEventLevel());
        dto.setEventDescription(model.getEventDescription());
        dto.setHelperEngineerCode(model.getHelperEngineerCode());
        dto.setHelperEngineerName(model.getHelperEngineerName());
        dto.setStatus(model.getStatus());

        model.getAnnex().stream().map(s -> this.malfunctionApplyFormAnnexConverter.toDTO(s)).filter(k -> null != k)
                .forEach(dto.getAnnex()::add);
        return dto;
    }

}
